package com.lottery.game.websocket.config;

import com.lottery.game.core.modules.service.app.ChatRecordService;
import com.lottery.game.core.modules.service.app.GameUserService;
import com.lottery.game.core.modules.service.app.OnlineAppContext;
import com.lottery.game.core.modules.service.app.OperationalSetService;
import com.lottery.game.core.modules.service.app.WebSocketService;
import com.lottery.game.core.modules.service.app.WebsocketMessageService;
import com.lottery.game.core.modules.service.app.v2.PassportService;
import com.lottery.game.core.modules.service.redis.RedisService;
import com.lottery.game.websocket.interceptor.InboundChannelInterceptorAdapter;
import com.lottery.game.websocket.interceptor.OutboundChannelInterceptorAdapter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

import lombok.extern.slf4j.Slf4j;

//import com.runshang.apps.cloudgame.websocket.interceptor.InboundChannelInterceptorAdapter;

/**
 * Websocket核心配置类
 * @author Jake
 *
 */
@Slf4j
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer {

    private final long HEART_BEAT = 5000L;

    @Autowired
	private GameUserService gameUserService;

	@Autowired
	private WebSocketService webSocketService;

	@Autowired
	private ChatRecordService chatRecordService;

	@Autowired
	private OnlineAppContext onlineAppContext;

	@Autowired
	private WebsocketMessageService websocketMessageService;

	@Autowired
	private RedisService redisFeignClient;
	
	@Autowired
	private PassportService passportService;
	
	@Value("${server.port}")
	private Integer serverPort;
	
	@Autowired
	private OperationalSetService operationalSetService;


    /**
     * websocket访问时用的端点
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
    	log.info("---registerStompEndpoints---");

    	registry.addEndpoint("/websocket")
    		.setAllowedOrigins("*")
			.withSockJS();
	}

    /**
     * Here注册MessageBorker
     */
	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		log.info("---add MessageBroker here---");
		ThreadPoolTaskScheduler te = new ThreadPoolTaskScheduler();
		te.initialize();
		te.setPoolSize(1);
		te.setThreadNamePrefix("ws-heartbeat-");

		registry.enableSimpleBroker("/topic", "/queue")
			.setHeartbeatValue(new long[]{HEART_BEAT,HEART_BEAT})  //加入心跳
			.setTaskScheduler(te);

		//配置前缀
		registry.setApplicationDestinationPrefixes("/app");
		registry.setUserDestinationPrefix("/user");
	}

	@Override
	public void configureClientInboundChannel(ChannelRegistration registration) {
//		registration.interceptors(new InboundChannelInterceptorAdapter(gameUserService, webSocketService, chatRecordService));
		//registration.interceptors(new InboundChannelInterceptorAdapter(websocketMessageService));
		registration.interceptors(new InboundChannelInterceptorAdapter(websocketMessageService,redisFeignClient, passportService, serverPort, onlineAppContext, operationalSetService));
	}

	@Override
	public void configureClientOutboundChannel(ChannelRegistration registration) {
		registration.interceptors(new OutboundChannelInterceptorAdapter(gameUserService, webSocketService, chatRecordService, onlineAppContext));
	}

}
